//package oop.demo.zoomanagementsystem;
package top.anka1.ims.ZooManagementSystem;
import java.util.ArrayList;
/**
 * Zoo Class - Manages a collection of animals
 * @author anka
 */
public class Zoo {
    public String ZooName;
    public Animal[] animals = new Animal[10];
    public Zoo(String ZooName) { // Constructor
        this.ZooName = ZooName;
    }
    public String addAnimal(Animal animal) { // Add an animal to the zoo
        for (int i = 0; i < animals.length; i++) {
            if (animals[i] == null) {
                animals[i] = animal;
                System.out.println("Animal '" + animal.Name + "' added to " + ZooName);
                return "Animal '" + animal.Name + "' added to " + ZooName;
            }
        }
        System.out.println("Zoo is full, cannot add more animals.");
        return ZooName + " is full, cannot add more animals.";
    }
    public String displayAnimals() { // Display all animals in the zoo
        String animalsString = "--- Animals in " + ZooName + " " + "(" + getCounter() + "/" + animals.length + ") ---\n";
        System.out.println("--- Animals in " + ZooName + " " + "(" + getCounter() + "/" + animals.length + ") ---");
        for (int i = 0; i < animals.length; i++) {
            if (animals[i] != null) {
                Animal animal = animals[i];
                animalsString += (i + 1 + ". Animal Name: " + animal.Name + ", Species: " + animal.Species + ", Age: " + animal.Age + "\n");
                System.out.println(i + 1 + ". Animal Name: " + animal.Name + ", Species: " + animal.Species + ", Age: " + animal.Age);
            }
        }
        animalsString += "--------------------------------------";
        System.out.println("--------------------------------------");
        System.out.println();
        return animalsString;
    }
    public String getName() { // Get the name of the zoo
        return ZooName;
    }
    public int getCounter() { // Get the number of animals in the zoo
        int count = 0;
        for (Animal animal : animals) {
            if (animal != null) {
                count++;
            }
        }
        return count;
    }
    public int findAnimal(String animalName) { // Find an animal by its name
        for (int i = 0; i < animals.length; i++) {
            if (animals[i] == null) {
                continue; // Skip null animals
            }
            if (animals[i].getName().equals(animalName)) {
                return i;
            }
        }
        return -1; // Not found
    }
    public void moveAnimal(String animalName, Zoo to, Logistics logistics) { // Move an animal to another zoo
        int index = findAnimal(animalName);
        if (index != -1) {
            Animal animal = animals[index];
            System.out.println("******** ANIMAL TRANSFER INVOICE ********");
            System.out.println("From: " + this.ZooName);
            System.out.println("To: " + to.ZooName);
            System.out.println("Animal Details:");
            System.out.println("Animal Name: " + animal.Name + ", Species: " + animal.Species + ", Age: " + animal.Age);
            System.out.println();
            System.out.println("----- Logistics Details -----");
            System.out.println("Vehicle Information: ");
            System.out.println("Name: " + logistics.vehicle.name + ", Price: ¥" + logistics.vehicle.price + ", Code: " + logistics.vehicle.code);
            System.out.println("Fuel Information: ");
            System.out.println("Name: " + logistics.fuel.name + ", Price: ¥" + logistics.fuel.price + ", Code: " + logistics.fuel.code);
            System.out.print("Caretakers: ");
            for (int i = 0; i < logistics.caretakers.length; i++) {
                System.out.print(logistics.caretakers[i]);
                if (i != logistics.caretakers.length - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println();
            System.out.println("Total Logistics Cost: ¥" + (logistics.vehicle.price + logistics.fuel.price));
            System.out.println("---------------------------");
            System.out.println();
            System.out.println("****************************************");
            System.out.println();
            to.addAnimal(animal);
            this.deleteAnimal(animalName);
            System.out.println("Animal '" + animalName + "' successfully moved from " + this.ZooName + " to " + to.ZooName);
        } else {
            System.out.println("Animal not found.");
        }
    }
    public String deleteAnimal(String animalName) { // Remove an animal from the zoo
        int index = findAnimal(animalName);
        if (index != -1) {
            animals[index] = null;
            System.out.println("Animal '" + animalName + "' removed from " + ZooName);
            return "Animal '" + animalName + "' removed from " + ZooName;
        } else {
            System.out.println("Animal not found.");
            return "Animal not found.";
        }
    }
    public Animal getAnimal(int index) { // Get an animal by its index
        if (index >= 0 && index < animals.length) {
            return animals[index];
        } else {
            return new Animal("Not found", "Not found", -1); // Return a default animal if index is out of bounds
        }
    }
    public void printAnimals() { // Print all animals in the zoo
        for (Animal animal : animals) {
            if (animal != null) {
                System.out.println("Animal Name: " + animal.Name);
                System.out.println("Animal Species: " + animal.Species);
                System.out.println("Animal Age: " + animal.Age);
            }
        }
    }
    public ArrayList<Animal> getAnimals() {
        ArrayList<Animal> animalList = new ArrayList<>();
        for (Animal animal : animals) {
            if (animal != null) {
                animalList.add(animal);
            }
        }
        return animalList;
    }
}
